package com.yang.service.adminimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketKeyWord;
import com.yang.bean.MarketKeyWordExample;
import com.yang.bean.common.BasePageInfo;
import com.yang.bean.common.CommonData;
import com.yang.bean.zdd.bo.CreateKeywordBo;
import com.yang.bean.zdd.bo.UpdateKeywordBo;
import com.yang.bean.zdd.vo.CreateKeywordVo;
import com.yang.mapper.MarketKeyWordMapper;
import com.yang.service.admin.KeywordService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName KeywordServiceImpl
 * @Description TODO 关键词模块
 * @Author zhangdongdong
 * @Date 2022/9/6 20:39
 * @Version 1.0
 */
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    MarketKeyWordMapper mapper;

    /**
     * @description: 查询keyword表
     * @param: info keyword url
     * @return: com.yang.bean.common.CommonData<com.yang.bean.MarketKeyWord>
     * @author zhangdongdong
     * @date: 2022/9/7 9:24
     */
    @Override
    public CommonData<MarketKeyWord> list(BasePageInfo info, String keyword, String url) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketKeyWordExample example = new MarketKeyWordExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketKeyWordExample.Criteria criteria = example.createCriteria();
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%"+keyword+ "%");
        }
        if (url != null && !"".equals(url)) {
            criteria.andUrlLike("%" + url + "%");
        }

        List<MarketKeyWord> list = mapper.selectByExample(example);
        PageInfo<MarketKeyWord> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * @description: 插入一个关键词
     * @param: keyword url isHot isDefault
     * @return: com.yang.bean.zdd.vo.CreateKeywordVo
     * @author zhangdongdong
     * @date: 2022/9/7 9:25
     */
    @Override
    @SneakyThrows
    public CreateKeywordVo create(CreateKeywordBo createKeywordBo) {
        //插入数据
        MarketKeyWord marketKeyWord = new MarketKeyWord();
        marketKeyWord.setKeyword(createKeywordBo.getKeyword());
        marketKeyWord.setUrl(createKeywordBo.getUrl());
        marketKeyWord.setIsHot(createKeywordBo.getIsHot());
        marketKeyWord.setIsDefault(createKeywordBo.getIsDefault());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(new Date());
        Date time = dateFormat.parse(date);
        marketKeyWord.setAddTime(time);
        marketKeyWord.setUpdateTime(time);
        marketKeyWord.setDeleted(false);
        mapper.insertSelective(marketKeyWord);
        CreateKeywordVo createKeywordVo = new CreateKeywordVo();
        createKeywordVo.setId(marketKeyWord.getId());
        createKeywordVo.setKeyword(createKeywordBo.getKeyword());
        createKeywordVo.setUrl(createKeywordBo.getUrl());
        createKeywordVo.setHot(createKeywordBo.getIsHot());
        createKeywordVo.setDefault(createKeywordBo.getIsDefault());
        createKeywordVo.setAddTime(date);
        createKeywordVo.setUpdateTime(date);
        return createKeywordVo;
    }

    @Override
    @SneakyThrows
    public MarketKeyWord update(UpdateKeywordBo updateKeywordBo) {

        MarketKeyWord marketKeyWord = new MarketKeyWord();

        marketKeyWord.setId(updateKeywordBo.getId());
        marketKeyWord.setKeyword(updateKeywordBo.getKeyword());
        marketKeyWord.setUrl(updateKeywordBo.getUrl());
        marketKeyWord.setIsHot(updateKeywordBo.isHot());
        marketKeyWord.setIsDefault(updateKeywordBo.isDefault());
        marketKeyWord.setSortOrder(updateKeywordBo.getSortOrder());
        marketKeyWord.setAddTime(updateKeywordBo.getAddTime());
        marketKeyWord.setUpdateTime(new Date());
        mapper.updateByPrimaryKey(marketKeyWord);

        return marketKeyWord;
    }

    @Override
    public void delete(MarketKeyWord marketKeyWord) {
        mapper.deleteByPrimaryKey(marketKeyWord.getId());
    }
}
